using UnityEngine;
using Random = UnityEngine.Random;

public class RectMeshAnim : MeshAnimBase
{
    private RectMesh[] _rectMeshes;
    private float[] _lengths;

    protected override void Init()
    {
        _rectMeshes = GetComponentsInChildren<RectMesh>();
        _count = _rectMeshes.Length;
        _lengths = new float[_count];
        for (int i = 0; i < _count; i++)
        {
            _lengths[i] = _rectMeshes[i].m_Length;
        }
    }

    protected override void SetBeginState()
    {
        for (int i = 0; i < _count; i++)
        {
            _rectMeshes[i].Length = 0f;
        }
    }

    protected override void PlayForwardAnimation()
    {
        for (var i = 0; i < _count; i++)
        {
            var duration = Random.Range(m_minDuration, m_maxDuration);
            var delay = Random.Range(m_minDelay, m_maxDelay);
            _rectMeshes[i].DoAnim(_lengths[i], duration, delay);
        }
    }

    protected override void PlayBackwardAnimation()
    {
        for (var i = 0; i < _count; i++)
        {
            var duration = Random.Range(m_minDuration, m_maxDuration);
            var delay = Random.Range(m_minDelay, m_maxDelay);
            _rectMeshes[i].DoAnim(0, duration, delay);
        }
    }
}
